﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Albon.Model;
namespace BLL
{
    public class OrderItemBLL
    {
        ItemClothBLL itemclothbll = new ItemClothBLL();
        DAL.OrderItemDAL orderitemdal = new DAL.OrderItemDAL();
        /// <summary>
        /// 添加一个临时订单项
        /// </summary>
        public int addOrderItem(OrderItem orderitem)
        {
            return orderitemdal.addOrderItem(orderitem);
        }
        /// <summary>
        /// 复制订单项
        /// </summary>
        public string copyItem(string olditemid)
        {
            return orderitemdal.copyItem(olditemid);
        }
        /// <summary>
        /// 更新临时表中数量和是否试身的信息
        /// </summary>
        public bool updateOrderItem(OrderItem orderitem)
        {
            return orderitemdal.updateOrderItem(orderitem);
        }
        /// <summary>
        /// 根据订单ID获取该订单的所有已完成的item
        /// </summary>
        public DataTable getItemByOrderID(string orderid)
        {
            return orderitemdal.getItem("orderID="+orderid+" and finished=1");
        }
        /// <summary>
        ///根据ID获取orderItem信息 
        /// </summary>
        public DataTable getItemByID(string orderitemid)
        {
            return orderitemdal.getItem("ID="+orderitemid);
        }
        /// <summary>
        /// 订单项布料是否已经选择
        /// </summary>
        public bool clothSelected(string orderitemid)
        {
            DataTable dt=itemclothbll.getItemCloth(orderitemid);
            bool ret = true;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["companyName"].ToString() == "")
                {
                    ret = false; break;
                }
            }
            return ret;
        }
        /// <summary>
        /// 删除一个订单项
        /// </summary>
        /// <param name="orderitemid"></param>
        public void deleteOrderItem(string orderitemid)
        {
            orderitemdal.deleteOrderItem(orderitemid);
        }

        /// <summary>
        /// 判断该订单项相应订单是否所有订单项都出现在了ShipMent表内，如果示，则表示已经完成出货，将订单状态改为Complete
        /// </summary>
        /// <param name="p"></param>
        public void completeOrder(int orderitemid)
        {
            bool mark = true;
            //获取该订单项对应的订单ID
            DataTable dt = getItemByID(orderitemid.ToString());
            string orderid = dt.Rows[0]["orderid"].ToString();
            DataTable dtItems = getItemByOrderID(orderid);
            //判断如果有未发货的订单项，则不可将订单状态设为已完成
            foreach(DataRow dr in dtItems.Rows)
            {
                DAL.ShipDAL shipdal=new DAL.ShipDAL();
                if(shipdal.getShip("orderItemID="+dr["ID"].ToString()).Rows.Count==0)
                {
                    mark = false;
                    break;
                }
            }
            if (mark)
            {
                DAL.OrderDAL orderdal = new DAL.OrderDAL();
                orderdal.completeOrder(orderid);
            }
        }

       
    }
}
